<?php

namespace VueRender\Support;

use VueRender\Fluent;

/**
 * @property string $type "state" | "ref" | "method" | "function"
 * @property string $key
 * @property string|array $value
 * @property mixed $default
 */
class Js extends Fluent
{

    protected array $types = ["bind","state","model", "ref", "method", "function", "watch", "computed" ];

    /**
     * @param string $type
     * @param string|array $value
     */
    public function __construct(string $type, string|array $value)
    {
        $this->type = $type;
        $this->value = $value;
        $this->default = null;
        $this->key = "";
    }

    /**
     * @param string $value
     * @param mixed $default
     * @return $this
     */
    public static function bind(string $value, mixed $default = null): static
    {
        $self = new self("bind", "state.".$value);
        $self->key = $value;
        $self->default = $default;
        return $self;
    }

    /**
     * @param string $value
     * @param mixed $default
     * @return $this
     */
    public static function state(string $value, mixed $default = null): static
    {
        $self = new self("state", "state.".$value);
        $self->key = $value;
        $self->default = $default;
        return $self;
    }

    /**
     * @param string $value
     * @param mixed $default
     * @return $this
     */
    public static function model(string $value, mixed $default = null): static
    {
        $self = new self("model", "state.".$value);
        $self->key = $value;
        $self->default = $default;
        return $self;
    }

    /**
     * @param string $value
     * @return $this
     */
    public static function ref(string $value): static
    {
        return new self("ref", $value);
    }

    /**
     * @param string $value
     * @return $this
     */
    public static function method(string $value): static
    {
        return new self("method", $value);
    }

    /**
     * @param string $stateField
     * @param string $callback
     * @return $this
     */
    public static function watch(string $stateField, string $callback): static
    {
        return new self("watch", [
            "key"=>$stateField, "callback"=>$callback
        ]);
    }

    /**
     * @param string $stateField
     * @param string $callback
     * @return $this
     */
    public static function computed(string $stateField, string $callback): static
    {
        return new self("computed", [
            "key"=>$stateField, "callback"=>$callback
        ]);
    }
}
